package com.ruoyi.wlxz.controller;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.wlxz.domain.Category;
import com.ruoyi.wlxz.service.ICategoryService;
import com.ruoyi.wlxz.tools.CategoryManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 类目Controller
 *
 * @author zlh
 * @date 2025-03-21
 */
@RestController
@RequestMapping("/wlxz/category")
public class CategoryController extends BaseController {
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private CategoryManager categoryManager;
    /**
     * 查询类目列表
     */
    @PreAuthorize("@ss.hasPermi('wlxz:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(Category category) {
        startPage();
        List<Category> list = categoryService.selectCategoryList(category);
        return getDataTable(list);
    }
    /**
     * 导出类目列表
     */
    @PreAuthorize("@ss.hasPermi('wlxz:category:export')")
    @Log(title = "类目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Category category) {
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        util.exportExcel(response, list, "类目数据");
    }
    /**
     * 获取类目详细信息
     */
    @PreAuthorize("@ss.hasPermi('wlxz:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(categoryService.selectCategoryById(id));
    }
    /**
     * 新增类目
     */
    @PreAuthorize("@ss.hasPermi('wlxz:category:add')")
    @Log(title = "类目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Category category) {
        try {
            String categoryId = categoryManager.CreateCategory(category.getCategoryName());
            category.setCategoryId(categoryId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return toAjax(categoryService.insertCategory(category));
    }
    /**
     * 修改类目
     */
    @PreAuthorize("@ss.hasPermi('wlxz:category:edit')")
    @Log(title = "类目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Category category) {
        return toAjax(categoryService.updateCategory(category));
    }
    /**
     * 删除类目
     */
    @PreAuthorize("@ss.hasPermi('wlxz:category:remove')")
    @Log(title = "类目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        Category category = categoryService.selectCategoryById(ids[0]);
        String categoryId = category.getCategoryId();
        try {
            categoryManager.DeleteCategory(categoryId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return toAjax(categoryService.deleteCategoryByIds(ids));
    }
}